package o2s.utils;

import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import org.apache.http.util.ByteArrayBuffer;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
import android.util.Log;

public class imageHelper {
	
	public static int downloadStage = 0;
	
	public static Bitmap getImageFromResource(Context mContext,String imagePath){
		InputStream buf;
		try {
			buf = mContext.getAssets().open(imagePath);
			Bitmap bitmap = BitmapFactory.decodeStream(buf);
			return bitmap;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
    public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
                .getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);
        final float roundPx = pixels;

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);

        return output;
    }
    
    public synchronized static boolean downloadFromUrl(String imageURL, String fileName , Context context) {  //this is the downloader method
        try {
        		//Started 
        		downloadStage = 1;
        		
                URL url = new URL(imageURL); //you can write here any link
                long startTime = System.currentTimeMillis();
                Log.d("ImageManager", "download begining");
                Log.d("ImageManager", "download url:" + url);
                Log.d("ImageManager", "downloaded file name:" + fileName);
                /* Open a connection to that URL. */
                URLConnection ucon = url.openConnection();

                /*
                 * Define InputStreams to read from the URLConnection.
                 */
                InputStream is = ucon.getInputStream();
                BufferedInputStream bis = new BufferedInputStream(is);

                /*
                 * Read bytes to the Buffer until there is nothing more to read(-1).
                 */
                ByteArrayBuffer baf = new ByteArrayBuffer(50);
                int current = 0;
                while ((current = bis.read()) != -1) {
                        baf.append((byte) current);
                }
                
                FileOutputStream fos;
                try {
        			fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
        			fos.write(baf.buffer());
        	        fos.close();
        		} catch (Exception e) {
        			// TODO Auto-generated catch block
        			e.printStackTrace();
        			//End with err 
            		downloadStage = 2;
        		}

                Log.d("ImageManager", "download ready in"
                                + ((System.currentTimeMillis() - startTime) / 1000)
                                + " sec");
                
              //End 
                return true;

        } catch (IOException e) {
                Log.d("ImageManager", "Error: " + e);
                //End with err 
        		downloadStage = 2;
        		return false;
        }
	}

    public static Bitmap readImageFromUrl(String filename , Context context){
    	String dataDir = context.getApplicationContext().getApplicationInfo().dataDir;
    	Bitmap bitmap = BitmapFactory.decodeFile(dataDir+"/files/"+filename);
        return bitmap;
    }
}